package com.ecelin;

import cn.hutool.core.swing.clipboard.ClipboardUtil;
import com.benjaminwan.ocrlibrary.OcrResult;
import io.github.mymonstercat.Model;
import io.github.mymonstercat.ocr.InferenceEngine;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class MainApp {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame("图片文字识别");
        jFrame.setSize(800, 600);
        jFrame.setLocation((int) ((Toolkit.getDefaultToolkit().getScreenSize().getWidth() - jFrame.getWidth()) / 2),
                (int) ((Toolkit.getDefaultToolkit().getScreenSize().getHeight() - jFrame.getHeight()) / 2));
        //创建文本区域,需要能滑动
        JTextArea jTextArea = new JTextArea();
        jTextArea.setRows(30);
        jTextArea.setColumns(60);
        JScrollPane scrollPane = new JScrollPane(jTextArea);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        JPanel jPanel = new JPanel();
        jPanel.add(scrollPane);
        jFrame.add(jPanel, BorderLayout.NORTH);
        JPanel panel2 = new JPanel();
        JButton jButton = new JButton("识别剪切板中截图");
        jButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                InferenceEngine engine = InferenceEngine.getInstance(Model.ONNX_PPOCR_V3);
                BufferedImage image = (BufferedImage) ClipboardUtil.getImage();
                if (image == null) {
                    jTextArea.setText("剪切板中没有图片信息,请将需要识别的图片存入剪切板!");
                    return;
                }
                File imgFile = new File(System.getProperty("java.io.tmpdir") + "simple-orc.png");
                try {
                    ImageIO.write(image, "png",imgFile);
                } catch (IOException ex) {
                    jTextArea.setText(ex.getLocalizedMessage());
                }
                OcrResult ocrResult = engine.runOcr(imgFile.getAbsolutePath());
                if (imgFile.exists()) {
                    boolean delete = imgFile.delete();
                }
                String trim = ocrResult.getStrRes().trim();
                jTextArea.setText(trim);
            }
        });
        JButton jButton1 = new JButton("将识别内容复制到剪切板");
        jButton1.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String text = jTextArea.getText();
                ClipboardUtil.setStr(text == null ? "" : text);
            }
        });

        panel2.add(jButton, BorderLayout.WEST);
        panel2.add(jButton1, BorderLayout.EAST);
        jFrame.add(panel2);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.pack();
        jFrame.setVisible(true);
    }
}
